const Clutter     = imports.gi.Clutter;
const Mx          = imports.gi.Mx;
const Octopus     = imports.gi.Octopus;
const Lang        = imports.lang;
const Signals     = imports.signals;
const BrowsePanel     = imports.browsePanel;
const CollectionPanel = imports.collectionPanel;
const SearchPanel     = imports.searchPanel;
const TracklistPanel  = imports.tracklistPanel;

const BARHEIGHT = 40;
const ICONSIZE = 30;

var TabEnum = {
	BROWSE: 0,
	COLLECTION: 1,
	SEARCH: 2
}

function MedialibPanel(container) {
	this._init(container);
}

MedialibPanel.prototype = {
	_init: function (container) {
		this.browsePanelLoadFlag = false;
		this.collectionPanelLoadFlag = false;

		this.medialibPanelStack = new Mx.Stack ({ name: 'medialibPanel-background' });
		container.add_child (this.medialibPanelStack);

		this.medialibPanelLayout = new Mx.BoxLayout ({ style_class: 'medialibPanel-layout',
		                                               orientation: Mx.Orientation.VERTICAL,
		                                               x_align: Clutter.ActorAlign.FILL,
		                                               y_align: Clutter.ActorAlign.FILL });
		this.medialibPanelStack.add_child (this.medialibPanelLayout);

		this.medialibPanelTabBar = new Mx.Stack ({ style_class: 'medialibPanel-tab-bar',
		                                           x_align: Clutter.ActorAlign.FILL,
		                                           y_align: Clutter.ActorAlign.START,
		                                           height: BARHEIGHT });
		this.medialibPanelLayout.add_child (this.medialibPanelTabBar);

		this.medialibPanelBrowsePanelTab = new Mx.Frame ({ style_class: 'medialibPanel-browse-tab',
		                                                   x_align: Clutter.ActorAlign.END,
		                                                   height: BARHEIGHT });
		this.medialibPanelTabBar.add_child (this.medialibPanelBrowsePanelTab);
		this.medialibPanelBrowsePanelButton = new Mx.Button ({ style_class: 'medialibPanel-browse-button',
		                                                       x_align: Clutter.ActorAlign.CENTER,
		                                                       y_align: Clutter.ActorAlign.CENTER,
		                                                       width:  ICONSIZE,
		                                                       height: ICONSIZE });
		this.medialibPanelBrowsePanelTab.add_child (this.medialibPanelBrowsePanelButton);
		this.medialibPanelBrowsePanelButton.connect ('clicked', Lang.bind(this, function () {
			this.setCurrentTab (TabEnum.BROWSE);
		}));
		this.medialibPanelBrowsePanelButton.connect ('enter-event', Lang.bind(this, function () {
			this.emit ('show-message-bar-text', "albumlist");
		}));
		this.medialibPanelBrowsePanelButton.connect ('leave-event', Lang.bind(this, function () {
			this.emit ('show-message-bar-text', "");
		}));

		this.medialibPanelCollectionPanelTab = new Mx.Frame ({ style_class: 'medialibPanel-collection-tab',
		                                                       x_align: Clutter.ActorAlign.END,
		                                                       height: BARHEIGHT });
		this.medialibPanelTabBar.add_child (this.medialibPanelCollectionPanelTab);
		this.medialibPanelCollectionPanelButton = new Mx.Button ({ style_class: 'medialibPanel-collection-button',
		                                                           x_align: Clutter.ActorAlign.CENTER,
		                                                           y_align: Clutter.ActorAlign.CENTER,
		                                                           width:  ICONSIZE,
		                                                           height: ICONSIZE });
		this.medialibPanelCollectionPanelTab.add_child (this.medialibPanelCollectionPanelButton);
		this.medialibPanelCollectionPanelButton.connect ('clicked', Lang.bind(this, function () {
			this.setCurrentTab (TabEnum.COLLECTION);
		}));
		this.medialibPanelCollectionPanelButton.connect ('enter-event', Lang.bind(this, function () {
			this.emit ('show-message-bar-text', "collections/playlists");
		}));
		this.medialibPanelCollectionPanelButton.connect ('leave-event', Lang.bind(this, function () {
			this.emit ('show-message-bar-text', "");
		}));

		this.medialibPanelSearchPanelTab = new Mx.Frame ({ style_class: 'medialibPanel-search-tab',
		                                                   x_align: Clutter.ActorAlign.END,
		                                                   height: BARHEIGHT });
		this.medialibPanelTabBar.add_child (this.medialibPanelSearchPanelTab);
		this.medialibPanelSearchPanelButton = new Mx.Button ({ style_class: 'medialibPanel-search-button',
		                                                       x_align: Clutter.ActorAlign.CENTER,
		                                                       y_align: Clutter.ActorAlign.CENTER,
		                                                       width:  ICONSIZE,
		                                                       height: ICONSIZE });
		this.medialibPanelSearchPanelTab.add_child (this.medialibPanelSearchPanelButton);
		this.medialibPanelSearchPanelButton.connect ('clicked', Lang.bind(this, function () {
			this.setCurrentTab (TabEnum.SEARCH);
		}));
		this.medialibPanelSearchPanelButton.connect ('enter-event', Lang.bind(this, function () {
			this.emit ('show-message-bar-text', "search");
		}));
		this.medialibPanelSearchPanelButton.connect ('leave-event', Lang.bind(this, function () {
			this.emit ('show-message-bar-text', "");
		}));

		this.medialibPanelContentStack = new Mx.Stack ({ style_class: 'medialibPanel-content',
		                                                 x_align: Clutter.ActorAlign.FILL,
		                                                 y_align: Clutter.ActorAlign.FILL });
		this.medialibPanelLayout.add_child (this.medialibPanelContentStack);

		//browsePanel
		this.browsePanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
		                                        y_align: Clutter.ActorAlign.FILL });
		this.medialibPanelContentStack.add_child (this.browsePanelFrame); 
		this.browsePanel = new BrowsePanel.BrowsePanel (this.browsePanelFrame);
		//collectionPanel
		this.collectionPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
		                                            y_align: Clutter.ActorAlign.FILL,
		                                            visible: false });
		this.medialibPanelContentStack.add_child (this.collectionPanelFrame); 
		this.collectionPanel = new CollectionPanel.CollectionPanel (this.collectionPanelFrame);
		//searchPanel
		this.searchPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
		                                        y_align: Clutter.ActorAlign.FILL,
		                                        y_expand: true });
		this.medialibPanelContentStack.add_child (this.searchPanelFrame); 
		this.searchPanel = new SearchPanel.SearchPanel (this.searchPanelFrame);
		//tracklistPanel
		this.tracklistPanelFrame = new Mx.Frame ({ x_align: Clutter.ActorAlign.FILL,
		                                           y_align: Clutter.ActorAlign.FILL });
		this.medialibPanelStack.add_child (this.tracklistPanelFrame);

		this.setCurrentTab (TabEnum.BROWSE);

		this.medialibPanelOverlay = new Mx.Frame ({ name: 'medialibPanel-overlay' });
		this.medialibPanelStack.add_child (this.medialibPanelOverlay);

		//connect to signals
		this.browsePanel.connect('load-finished', Lang.bind(this, function () {
			this.browsePanelLoadFlag = true;
			if (this.collectionPanelLoadFlag) this.emit ('load-finished');
		}));
		this.collectionPanel.connect('load-finished', Lang.bind(this, function () {
			this.collectionPanelLoadFlag = true;
			if (this.browsePanelLoadFlag) this.emit ('load-finished');
		}));
		//signals browsePanel
		this.browsePanel.connect('grab-key-focus', Lang.bind(this, function () {
			this.keyFocusActor = this.browsePanel;
			this.emit ('grab-key-focus');
		}));
		this.browsePanel.connect('tracklist-panel-show', Lang.bind(this, function (browsePanel, /*Album*/album_model) {
			this.tracklistPanel = new TracklistPanel.TracklistPanel (this.tracklistPanelFrame, album_model);
			this.tracklistPanel.connect('grab-key-focus', Lang.bind(this, function () {
				this.keyFocusActor = this.tracklistPanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('tracklist-panel-close', Lang.bind(this, function () {
				this.keyFocusActor = this.browsePanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('show-message-bar-text', Lang.bind(this, function (tracklistPanel, msg) {
				this.emit ('show-message-bar-text', msg);
			}));
			this.tracklistPanel.tracklistPanelShow ();
		}));
		this.browsePanel.connect('show-message-bar-text', Lang.bind(this, function (browsePanel, msg) {
			this.emit ('show-message-bar-text', msg);
		}));
		//signals collectionPanel
		this.collectionPanel.connect('grab-key-focus', Lang.bind(this, function () {
			this.keyFocusActor = this.collectionPanel;
			this.emit ('grab-key-focus');
		}));
		this.collectionPanel.connect('new-collection', Lang.bind(this, function () {
			this.setCurrentTab (TabEnum.SEARCH);
		}));
		this.collectionPanel.connect('new-playlist', Lang.bind(this, function () {
			let newPlaylist = new Octopus.Playlist ();
			this.tracklistPanel = new TracklistPanel.TracklistPanel (this.tracklistPanelFrame, newPlaylist);
			this.tracklistPanel.connect('grab-key-focus', Lang.bind(this, function () {
				this.keyFocusActor = this.tracklistPanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('tracklist-panel-close', Lang.bind(this, function () {
				this.keyFocusActor = this.collectionPanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('show-message-bar-text', Lang.bind(this, function (tracklistPanel, msg) {
				this.emit ('show-message-bar-text', msg);
			}));
			this.tracklistPanel.tracklistPanelShow ();
			
		}));
		this.collectionPanel.connect('tracklist-panel-show', Lang.bind(this, function (collectionPanel, /*Collection/playlist*/tracklist_model) {
			this.tracklistPanel = new TracklistPanel.TracklistPanel (this.tracklistPanelFrame, tracklist_model);
			this.tracklistPanel.connect('grab-key-focus', Lang.bind(this, function () {
				this.keyFocusActor = this.tracklistPanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('tracklist-panel-close', Lang.bind(this, function () {
				this.keyFocusActor = this.collectionPanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('show-message-bar-text', Lang.bind(this, function (tracklistPanel, msg) {
				this.emit ('show-message-bar-text', msg);
			}));
			this.tracklistPanel.tracklistPanelShow ();
		}));
		this.collectionPanel.connect('show-message-bar-text', Lang.bind(this, function (collectionPanel, msg) {
			this.emit ('show-message-bar-text', msg);
		}));
		//signals searchPanel
		this.searchPanel.connect('grab-key-focus', Lang.bind(this, function () {
			this.keyFocusActor = this.searchPanel;
			this.emit ('grab-key-focus');
		}));
		this.searchPanel.connect('tracklist-panel-show', Lang.bind(this, function (searchPanel, /*Collection*/tracklist_model) {
			this.tracklistPanel = new TracklistPanel.TracklistPanel (this.tracklistPanelFrame, tracklist_model);
			this.tracklistPanel.connect('grab-key-focus', Lang.bind(this, function () {
				this.keyFocusActor = this.tracklistPanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('tracklist-panel-close', Lang.bind(this, function () {
				this.keyFocusActor = this.searchPanel;
				this.emit ('grab-key-focus');
			}));
			this.tracklistPanel.connect('show-message-bar-text', Lang.bind(this, function (tracklistPanel, msg) {
				this.emit ('show-message-bar-text', msg);
			}));
			this.tracklistPanel.tracklistPanelShow ();
		}));
		this.searchPanel.connect('show-message-bar-text', Lang.bind(this, function (searchPanel, msg) {
			this.emit ('show-message-bar-text', msg);
		}));
	},

	setCurrentTab: function (tab) {
		switch (tab) {
			case TabEnum.BROWSE:
				//tab-bar
				this.medialibPanelBrowsePanelTab.style_pseudo_class_add ("current-tab");
				this.medialibPanelCollectionPanelTab.style_pseudo_class_remove ("current-tab");
				this.medialibPanelSearchPanelTab.style_pseudo_class_remove ("current-tab");
				this.medialibPanelTabBar.set_child_above_sibling (this.medialibPanelCollectionPanelTab, this.medialibPanelSearchPanelTab);
				this.medialibPanelTabBar.set_child_above_sibling (this.medialibPanelBrowsePanelTab, this.medialibPanelCollectionPanelTab);
				//content
				if (this.tracklistPanelFrame.get_n_children () > 0) this.tracklistPanel.tracklistPanelClose ();
				this.browsePanelFrame.visible = true;
				this.collectionPanelFrame.visible = false;
				this.searchPanelFrame.visible = false;
				this.keyFocusActor = this.browsePanel;
				break;
			case TabEnum.COLLECTION:
				//tab-bar
				this.medialibPanelBrowsePanelTab.style_pseudo_class_remove ("current-tab");
				this.medialibPanelCollectionPanelTab.style_pseudo_class_add ("current-tab");
				this.medialibPanelSearchPanelTab.style_pseudo_class_remove ("current-tab");
				this.medialibPanelTabBar.set_child_above_sibling (this.medialibPanelBrowsePanelTab, this.medialibPanelSearchPanelTab);
				this.medialibPanelTabBar.set_child_above_sibling (this.medialibPanelCollectionPanelTab, this.medialibPanelSearchPanelTab);
				this.medialibPanelTabBar.set_child_above_sibling (this.medialibPanelCollectionPanelTab, this.medialibPanelBrowsePanelTab);
				//content
				if (this.tracklistPanelFrame.get_n_children () > 0) this.tracklistPanel.tracklistPanelClose ();
				this.browsePanelFrame.visible = false;
				this.collectionPanelFrame.visible = true;
				this.searchPanelFrame.visible = false;
				this.keyFocusActor = this.collectionPanel;
				break;
			case TabEnum.SEARCH:
				//tab-bar
				this.medialibPanelBrowsePanelTab.style_pseudo_class_remove ("current-tab");
				this.medialibPanelCollectionPanelTab.style_pseudo_class_remove ("current-tab");
				this.medialibPanelSearchPanelTab.style_pseudo_class_add ("current-tab");
				this.medialibPanelTabBar.set_child_above_sibling (this.medialibPanelCollectionPanelTab, this.medialibPanelBrowsePanelTab);
				this.medialibPanelTabBar.set_child_above_sibling (this.medialibPanelSearchPanelTab, this.medialibPanelCollectionPanelTab);
				//content
				if (this.tracklistPanelFrame.get_n_children () > 0) this.tracklistPanel.tracklistPanelClose ();
				this.browsePanelFrame.visible = false;
				this.collectionPanelFrame.visible = false;
				this.searchPanelFrame.visible = true;
				this.keyFocusActor = this.searchPanel;
				this.searchPanel.searchPanelGtkEntry.grab_focus ();
				break;
			default: break;
		}
		this.emit ('grab-key-focus');
	},

	load: function () {
		this.browsePanelLoadFlag = false;
		this.collectionPanelLoadFlag = false;
		this.browsePanel.load ();
		this.collectionPanel.load ();
	}
};

Signals.addSignalMethods(MedialibPanel.prototype);
